/**
 * 库存实体
 * 作者：AI Assistant
 * 创建时间：2024-03-14
 * 描述：商品库存信息实体，记录商品的库存数量和状态
 */
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, OneToMany, ManyToOne } from 'typeorm';
import { ProductSku } from './product-sku.entity';
import { InventoryLog } from './inventory-log.entity';

export enum InventoryStatus {
  IN_STOCK = 'in_stock',    // 有库存
  LOW_STOCK = 'low_stock',  // 库存不足
  OUT_OF_STOCK = 'out_of_stock', // 无库存
  DISCONTINUED = 'discontinued'   // 已下架
}

@Entity('inventories')
export class Inventory {
  @PrimaryGeneratedColumn()
  id!: number;

  @ManyToOne(() => ProductSku, sku => sku.inventory)
  sku!: ProductSku;

  @Column()
  quantity!: number;

  @Column({ type: 'enum', enum: InventoryStatus, default: InventoryStatus.IN_STOCK })
  status: InventoryStatus = InventoryStatus.IN_STOCK;

  @Column({ default: 10 })
  low_stock_threshold: number = 10;

  @Column({ type: 'text', nullable: true })
  notes: string = '';

  @Column({ default: true })
  is_active: boolean = true;

  @OneToMany(() => InventoryLog, log => log.sku)
  logs!: InventoryLog[];

  @CreateDateColumn()
  created_at!: Date;

  @UpdateDateColumn()
  updated_at!: Date;
}